package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcSafeLogMapper;
import com.csun.cmny.provider.dto.OmcSafeLogDto;
import com.csun.cmny.provider.model.domain.OmcSafeLog;
import com.csun.cmny.provider.service.OmcSafeLogService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-08 14:11
 * @Description: the class omc safe log service impl
 */
@Service
@Transactional
public class OmcSafeLogServiceImpl extends BaseService<OmcSafeLog> implements OmcSafeLogService {

    @Resource
    private OmcSafeLogMapper omcSafeLogMapper;

    /**
     * 保存安全日志
     *
     * @param omcSafeLogDto the omc safe log dto
     * @param loginAuthDto  the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer saveSafeLog(OmcSafeLogDto omcSafeLogDto, LoginAuthDto loginAuthDto) {

        OmcSafeLog omcSafeLogQuery = new OmcSafeLog();
        omcSafeLogQuery.setGroupId(loginAuthDto.getGroupId());
        omcSafeLogQuery.setDate(omcSafeLogDto.getDate());
        omcSafeLogQuery = omcSafeLogMapper.selectOne(omcSafeLogQuery);

        if (PublicUtil.isEmpty(omcSafeLogQuery)) {
            // 不存在 添加安全日志
            omcSafeLogQuery = new OmcSafeLog();
        }

        omcSafeLogQuery.setUpdateInfo(loginAuthDto);
        omcSafeLogQuery.setUserId(loginAuthDto.getUserId());
        omcSafeLogQuery.setUserName(loginAuthDto.getUserName());
        omcSafeLogQuery.setDate(omcSafeLogDto.getDate());
        omcSafeLogQuery.setSafeLog(omcSafeLogDto.getSafeLog());
        omcSafeLogQuery.setGroupId(loginAuthDto.getGroupId());

        if (omcSafeLogQuery.isNew()) {
            return omcSafeLogMapper.insertSelective(omcSafeLogQuery);
        } else {
            return omcSafeLogMapper.updateByPrimaryKeySelective(omcSafeLogQuery);
        }
    }
}
